﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS Overseas ResourceInfo
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-30
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.CIS2.Overseas
{
    /// <summary>
    ///  CIS Overseas ResourceInfo
    /// </summary>
    public partial class ResourcesInfo : EntityPage
    {
        private UserRoles LoadRoles()
        {
            UserRoles right = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
            if (!right.IsDomesticAdmin && right.IsDomesticSales && (User as CSPrincipal).AccessLevel == AccessLevel.Write)
                right.IsDomesticCommercial = true;
            return right;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //UserRoles userRole = LoadRoles();
            
            //if (userRole.IsOverseaSales||userRole.IsOverseaAdmin)
            //{
            //    this.edsBranchContact.Where = "it.Module and !it.Disabled";
            //}
            //else
            //{
            //    this.edsBranchContact.Where = "!it.Module and !it.Disabled";
            //}
            this.edsBranchContact.Where = "it.Module and !it.Disabled";
        }

       
        protected override EntityDataSource EntitySource
        {
            get { return this.edsBranchContact; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntitySearchBtn
        {
            get { return this.Searchbtn; }
        }

        protected override Button EntityClearBtn
        {
            get { return this.Cleanbtn; }
        }

        protected override GridView EntityGrid
        {
            get { return this.gvBranchContact; }
        }

        protected override DetailsView EntityDetails
        {
            get { return this.dvBranchContact; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            BranchContactsHelper.SetBranchContactsDisabled(entityKey);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "BranchContact disabled. id=" + entityKey + "; name=" + BranchContactsHelper.getName(entityKey));
        }

        protected override string GetEntityWhereExpression(string keyword)
        {
            return "it.Name like N'%" + keyword + "%'";

        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {

            var dc = e.Context;
            var branchContact = e.Entity as BranchContact;
            UserRoles userRole = LoadRoles();
            if (userRole.IsDomesticAdmin)
            {
                branchContact.Module =false;
            }
            if (userRole.IsOverseaAdmin)
            {
                branchContact.Module = true;
            }
            branchContact.Creator = (User as CSPrincipal).CIL;
            branchContact.Date = DateTime.Now;

            FileUpload file = this.dvBranchContact.FindControl("File") as FileUpload;

            BranchContactsHelper.BranchContactUpload(dc, branchContact, file);
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var branchContact = e.Entity as BranchContact;


            branchContact.Creator = (User as CSPrincipal).CIL;
            branchContact.Date = DateTime.Now;
            FileUpload file = this.dvBranchContact.FindControl("File") as FileUpload;

            BranchContactsHelper.BranchContactUpload(dc, branchContact, file);
        }


        protected void edsBranchContact_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var branchContact = e.Entity as BranchContact;


            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "ResourceInfo inserted. id=" + branchContact.DirectoryID + "; name=" + branchContact.Name + "; filePath=" + branchContact.FilePath);
        }

        protected void edsBranchContact_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var branchContact = e.Entity as BranchContact;

            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "ResourceInfo updated. id=" + branchContact.DirectoryID + "; name=" + branchContact.Name + "; filePath=" + branchContact.FilePath);
        }


    }
}
